package day2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class ch1 {

    //3
    public int lengthOfLongestSubstring(String s) {

        HashSet<Character> set = new HashSet<>();
        int max = 0;
        int lt = 0;
        for (int rt = 0; rt < s.length(); rt++) {
            char ch = s.charAt(rt);

            if(set.contains(ch)){
                while (set.contains(ch)&& !set.isEmpty()){
                    set.remove(s.charAt(lt));
                    lt++;
                }
            }
            set.add(ch);
            max = Math.max(max,set.size());


        }
        return max;
    }

    //438
    public List<Integer> findAnagrams(String s, String p) {


        int len = p.length();
        List<Integer> list = new ArrayList<>();
        HashMap<Character,Integer> map = new HashMap<>();
        HashMap<Character,Integer> exp = new HashMap<>();


        if(s.length()<p.length()){
            return  list;
        }

        for (int i=0;i<p.length();i++){
            int num = exp.getOrDefault(p.charAt(i),0);
            exp.put(p.charAt(i),num+1);
        }

        for (int i = 0; i < len-1; i++) {
            int num = map.getOrDefault(s.charAt(i),0);
            map.put(s.charAt(i),num+1);
        }

        for (int i = len-1; i < s.length(); i++) {
            int num = map.getOrDefault(s.charAt(i),0);
            map.put(s.charAt(i),num+1);
            if(cal(map,exp)){
                list.add(i-len+1);
            }
            int n = map.get(s.charAt(i-len+1));
            map.put(s.charAt(i-len+1),n-1);

        }

        return list;



    }

    public boolean cal(HashMap<Character,Integer> map,HashMap<Character,Integer> exp){
        for(Character ch:exp.keySet()){
            if(!map.containsKey(ch)){
                return false;
            }else {
                if(!map.get(ch).equals(exp.get(ch))){
                    return false;
                }
            }
        }

        return true;
    }




    public static void main(String[] args) {

    }

}
